<template>
  <div class="login-container">
    <div class="login-form">
      <div class="form-wrapper">
        <div class="login-title">欢迎登录<span class="app-name">链农</span>系统</div>
        <el-form ref="userForm" :model="userForm" :rules="rules" label-width="0px" class="login-form-content">
          <el-form-item prop="username">
            <el-input v-model="userForm.username" autocomplete="off" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="userForm.password" type="password" autocomplete="off" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password></el-input>
          </el-form-item>
          <el-form-item prop="identifyCode">
            <div class="verify-code-container">
              <el-input v-model="userForm.identifyCode" placeholder="请输入验证码" prefix-icon="el-icon-key"></el-input>
              <div class="verify-code" @click="refreshCode">
                <SIdentity :identify-code="identifyCode"></SIdentity>
              </div>
            </div>
          </el-form-item>
          <el-form-item class="btn-container">
            <el-button type="primary" @click="login('userForm')" class="login-btn">登录</el-button>
            <el-button type="text" @click="$router.push('/register')" class="register-btn">还没有账号？立即注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>

</template>

<script>
import SIdentity from "@/components/SIdentity";
import {setRoutes} from "@/router";

export default {
  name: "Login",
  components: {
    SIdentity
  },
  mounted() {
    this.refreshCode()
  },
  data() {
    return {
      userForm: {},
      identifyCode: '',
      identifyCodes: '1234567890zxcvbnmasdfghjklqwertyuiopQWERTYUIPOLSADFGHJ',
      rules: {
        username: [
          {required: true, message: '请输入用户名称', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 50, message: '长度不能小于6位', trigger: 'blur'}
        ],
        identifyCode: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 4, max: 4, message: '验证码长度为4位', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //提交数据
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 暂时注释掉验证码验证
          // if (this.identifyCode.toUpperCase() !== this.userForm.identifyCode.toUpperCase()) {
          //   this.$message.warning("验证码输入错误，请重新输入");
          //   return;
          // }
          
          // 只发送必要的数据
          const loginData = {
            username: this.userForm.username,
            password: this.userForm.password
          };
          
          console.log("发送登录请求数据:", JSON.stringify(loginData));
          
          this.request.post("/user/login", loginData).then(res => {
            console.log("登录响应:", res);
            if (res.code !== '200') {
              this.$message.error(res.msg);
            } else {
              localStorage.setItem("user", JSON.stringify(res.data))
              //保存菜单信息
              localStorage.setItem("menus", JSON.stringify(res.data.menuList))
              this.$message.success("登录成功");
              setRoutes();
              this.$router.push("/")
            }
          }).catch(err => {
            console.error("登录错误:", err);
          })
        } else {
          console.log('输入数据不正确，请重新输入');
          return false;
        }
      });
    },
    // 重置数据
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 验证码方法
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[
            this.randomNum(0, this.identifyCodes.length)
            ]
      }
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url("../assets/background.png");
  background-size: cover;
  background-position: center;
  position: relative;
}

.login-container::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(3px);
}

.login-form {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 450px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
  position: relative;
  z-index: 1;
  animation: fadeIn 0.8s ease-in-out;
  padding: 40px;
}

.form-wrapper {
  width: 100%;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-title {
  font-size: 28px;
  margin-bottom: 40px;
  font-weight: 600;
  text-align: center;
  color: #333;
  position: relative;
}

.app-name {
  color: #409EFF;
  font-weight: 700;
  margin: 0 5px;
}

.login-title::after {
  content: "";
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: #409EFF;
  border-radius: 2px;
}

.login-form-content {
  width: 100%;
}

.el-input {
  margin-bottom: 5px;
}

.el-input >>> .el-input__inner {
  height: 45px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  transition: all 0.3s;
}

.el-input >>> .el-input__inner:focus {
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.verify-code-container {
  display: flex;
  align-items: center;
}

.verify-code-container .el-input {
  flex: 1;
}

.verify-code {
  margin-left: 10px;
  cursor: pointer;
  border: 1px solid #e0e0e0;
  border-radius: 5px;
  overflow: hidden;
  height: 45px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
  background-color: white;
}

.verify-code:hover {
  border-color: #409EFF;
  transform: scale(1.02);
}

.btn-container {
  margin-top: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-btn {
  width: 100%;
  height: 45px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
  background: linear-gradient(to right, #409EFF, #66b1ff);
  border: none;
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
  transition: all 0.3s;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(64, 158, 255, 0.4);
  background: linear-gradient(to right, #3a8ee6, #5aa0e6);
}

.register-btn {
  margin-top: 15px;
  color: #606266;
  font-size: 14px;
  transition: all 0.3s;
}

.register-btn:hover {
  color: #409EFF;
  transform: translateY(-2px);
}

@media screen and (max-width: 576px) {
  .login-form {
    width: 90%;
    padding: 30px 20px;
  }
}
</style>
